<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java项目开发笔记_04 | MoneyLee’s Blog</title><meta name="author" content="李财"><meta name="copyright" content="李财"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Java项目开发笔记04-0x的代码在：gitee 设计应用的核心数据库表-ER图简介：  在线教育站点速揽 xdclass.net ER图只是回顾： 实体对象：矩形 属性：椭圆 关系：菱形   核心库表 video chapter episode user video_order video_banner     JavaWeb通过原生jdbc访问数据库 原生jdbc访问数据库步骤  加载JDB">
<meta property="og:type" content="article">
<meta property="og:title" content="Java项目开发笔记_04">
<meta property="og:url" content="https://moneylee.gitee.io/blog/2020/06/08/Java%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0_04/index.html">
<meta property="og:site_name" content="MoneyLee’s Blog">
<meta property="og:description" content="Java项目开发笔记04-0x的代码在：gitee 设计应用的核心数据库表-ER图简介：  在线教育站点速揽 xdclass.net ER图只是回顾： 实体对象：矩形 属性：椭圆 关系：菱形   核心库表 video chapter episode user video_order video_banner     JavaWeb通过原生jdbc访问数据库 原生jdbc访问数据库步骤  加载JDB">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg">
<meta property="article:published_time" content="2020-06-08T11:19:10.000Z">
<meta property="article:modified_time" content="2020-07-09T14:09:29.000Z">
<meta property="article:author" content="李财">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://moneylee.gitee.io/blog/2020/06/08/Java%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0_04/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Java项目开发笔记_04',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-07-09 22:09:29'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="MoneyLee’s Blog" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2019/11/10/SYvXMgCFuTNOVeZ.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">47</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fa fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/bbs/"><i class="fa-fw fa fa-coffee"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 影音</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/music"><i class="fa-fw /music/"></i><span> 0</span></a></li><li><a class="site-page child" href="/blog/movies"><i class="fa-fw /movies/"></i><span> 1</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">MoneyLee’s Blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fa fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/bbs/"><i class="fa-fw fa fa-coffee"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 影音</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/music"><i class="fa-fw /music/"></i><span> 0</span></a></li><li><a class="site-page child" href="/blog/movies"><i class="fa-fw /movies/"></i><span> 1</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Java项目开发笔记_04</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-06-08T11:19:10.000Z" title="发表于 2020-06-08 19:19:10">2020-06-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-07-09T14:09:29.000Z" title="更新于 2020-07-09 22:09:29">2020-07-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/Java/">Java</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Java项目开发笔记_04"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p><strong>Java项目开发笔记04-0x的代码在：</strong><a target="_blank" rel="noopener" href="https://gitee.com/moneylee/java_learning__02">gitee</a></p>
<h2 id="设计应用的核心数据库表-ER图"><a href="#设计应用的核心数据库表-ER图" class="headerlink" title="设计应用的核心数据库表-ER图"></a>设计应用的核心数据库表-ER图</h2><p><strong>简介：</strong></p>
<ul>
<li>在线教育站点速揽 xdclass.net</li>
<li>ER图只是回顾：<ul>
<li>实体对象：矩形</li>
<li>属性：椭圆</li>
<li>关系：菱形</li>
</ul>
</li>
<li>核心库表<ul>
<li>video</li>
<li>chapter</li>
<li>episode</li>
<li>user</li>
<li>video_order</li>
<li>video_banner</li>
</ul>
</li>
</ul>
<hr>
<h3 id="JavaWeb通过原生jdbc访问数据库"><a href="#JavaWeb通过原生jdbc访问数据库" class="headerlink" title="JavaWeb通过原生jdbc访问数据库"></a>JavaWeb通过原生jdbc访问数据库</h3><ul>
<li><p>原生jdbc访问数据库步骤</p>
<ul>
<li>加载JDBC驱动程序</li>
<li>创建数据库的连接</li>
<li>创建prepareStatement</li>
<li>执行SQL语句</li>
<li>处理结果集</li>
<li>关闭jdbc对象资源</li>
</ul>
</li>
<li><p>SpringBoot项目测试原生JDBC连接</p>
<ul>
<li>加载依赖包</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">		&lt;dependency&gt;</span><br><span class="line">			&lt;groupId&gt;mysql&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;</span><br><span class="line">		&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>main函数测试</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">package com.moneylee.xdstudy;</span><br><span class="line"></span><br><span class="line">import java.sql.Connection;</span><br><span class="line">import java.sql.DriverManager;</span><br><span class="line">import java.sql.ResultSet;</span><br><span class="line">import java.sql.Statement;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @className: JdbcApp</span><br><span class="line"> * @Description: TODO</span><br><span class="line"> * @Author moneylee</span><br><span class="line"> * @Date 2020-07-03 09:23</span><br><span class="line"> * @Version 1.0</span><br><span class="line"> **/</span><br><span class="line">public class JdbcApp &#123;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * - 加载JDBC驱动程序</span><br><span class="line">     * - 创建数据库的连接</span><br><span class="line">     * - 创建prepareStatement</span><br><span class="line">     * - 执行SQL语句</span><br><span class="line">     * - 处理结果集</span><br><span class="line">     * - 关闭jdbc对象资源</span><br><span class="line">     * @param args</span><br><span class="line">     */</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) throws Exception &#123;</span><br><span class="line"></span><br><span class="line">        Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);</span><br><span class="line">        //连接</span><br><span class="line">        String url = &quot;jdbc:mysql://127.0.0.1:3306/xdclass?useUnicode=true&amp;charaterEncoding=utf-8&amp;useSSL=false&quot;;</span><br><span class="line"></span><br><span class="line">        String username = &quot;root&quot;;</span><br><span class="line">        String password = &quot;12345678&quot;;</span><br><span class="line">        //获取连接对象，并连接数据库</span><br><span class="line">        Connection connection = DriverManager.getConnection(url, username, password);</span><br><span class="line"></span><br><span class="line">        Statement statement = connection.createStatement();</span><br><span class="line"></span><br><span class="line">        ResultSet resultSet = statement.executeQuery(&quot;select * from video&quot;);</span><br><span class="line"></span><br><span class="line">        while (resultSet.next()) &#123;</span><br><span class="line"></span><br><span class="line">            System.out.println(&quot;视频标题&quot; + resultSet.getString(&quot;title&quot;));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        statement.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="原生JDBC访问数据库缺点和ORM框架介绍"><a href="#原生JDBC访问数据库缺点和ORM框架介绍" class="headerlink" title="原生JDBC访问数据库缺点和ORM框架介绍"></a>原生JDBC访问数据库缺点和ORM框架介绍</h2><ul>
<li>原生jdbc缺点：<ul>
<li>每次要加载驱动，连接信息是硬编码</li>
<li>sql和java代码耦合在一起，改动不易</li>
<li>参数设置缺少灵活性</li>
<li>处理结果麻烦。不能自动映射到实体类</li>
<li>连接资源不能复用，资源浪费</li>
</ul>
</li>
<li>什么是ORM框架<ul>
<li>对数据库的表和POJO对象做映射的框架</li>
</ul>
</li>
<li>市面上的ORM框架<ul>
<li>hibernate<ul>
<li>比较笨重，sql调优麻烦</li>
<li>不推荐</li>
</ul>
</li>
<li>JPA - Spring Data JPA<ul>
<li>Java persistrnce API，轻量级，部分中小项目适合</li>
<li>次推荐</li>
</ul>
</li>
<li>mybatis<ul>
<li>半自动化（半ORM框架），便于写sql，轻量级，广泛使用</li>
<li>推荐</li>
</ul>
</li>
</ul>
</li>
</ul>
<hr>
<h3 id="新版ORM框架Mybatis3-x基础知识"><a href="#新版ORM框架Mybatis3-x基础知识" class="headerlink" title="新版ORM框架Mybatis3.x基础知识"></a>新版ORM框架Mybatis3.x基础知识</h3><ul>
<li>什么是Mybatis<ul>
<li>Apahce的一个开源项目</li>
<li>一款优秀的持久层框架，它支持自定义sql，存储过程及高级映射</li>
<li>免除了几乎所有JDBC代码以及设置参数和获取结果集的工作</li>
<li>通过简单的xml或注解来配置和映射Java对象到数据库中的记录</li>
</ul>
</li>
<li>核心流程：<ul>
<li>每个基于Mybatis的应用都是一个以一个 SqseesionFactory的实例为核心</li>
<li>SqseesionFactory的实例可以通过SqseesionFactoryBuilder获得</li>
<li>SqseesionFactoryBuilder可以从XML配置文件或一个预先配置的Configuration实例来构建出SqseesionFactory实例</li>
<li>工厂设计模式里面需要获取Sqseesion，里面提供了在数据库执行SQL命令所需的方法</li>
</ul>
</li>
</ul>
<hr>
<h3 id="Mybatis开发必备控制台打印SQL"><a href="#Mybatis开发必备控制台打印SQL" class="headerlink" title="Mybatis开发必备控制台打印SQL"></a>Mybatis开发必备控制台打印SQL</h3><ul>
<li>Mybatis使用流程<ul>
<li>创建mybatis-config.xml全局配置文件</li>
<li>创建XXXMapper.xml配置文件</li>
<li>创建SqlSessionFactory创建SqlSession对象</li>
<li>用SqlSession执行增删改查CRUD</li>
</ul>
</li>
<li>内置的日志工厂提供日志功能，使用log4j配置打印sql，添加依赖：</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.26<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>在应用的classpath中创建名称为log4j.properties的文件</li>
</ul>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">log4j.rootLogger</span>=<span class="string">ERROR, stdout</span></span><br><span class="line"></span><br><span class="line"><span class="meta">log4j.logger.net.xdclass</span>=<span class="string">DEBUG</span></span><br><span class="line"><span class="comment">#trace内容会打印更多</span></span><br><span class="line"><span class="comment">#log4j.logger.net.xdclass=TRACE</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#细化到打印某个mapper</span></span><br><span class="line"><span class="comment">#log4j.logger.net.xdclass.online_class.dao.VideoMapper.selectById=TRACE</span></span><br><span class="line"></span><br><span class="line"><span class="meta">log4j.appender.stdout</span>=<span class="string">org.apache.log4j.ConsoleAppender</span></span><br><span class="line"><span class="meta">log4j.appender.stdout.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="meta">log4j.appender.stdout.layout.ConversionPattern</span>=<span class="string">%5p [%t] - %m%n</span></span><br></pre></td></tr></table></figure>



<hr>
<h3 id="Mybatis参数别名使用"><a href="#Mybatis参数别名使用" class="headerlink" title="Mybatis参数别名使用"></a>Mybatis参数别名使用</h3><ul>
<li><p>常用的查询</p>
<ul>
<li>默认参数查询<ul>
<li>单个参数，可以使用别名，也可以使用默认的名称，默认名称的话可以随意</li>
</ul>
</li>
<li>使用参数别名</li>
<li>迷糊查询</li>
<li>mysql自带函数使用</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectByPointAndTitleLike&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;com.moneylee.xdstudy_02.domain.Video&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">     select * from video where point=#&#123;point&#125; and title like concat(&#x27;%&#x27;, #&#123;title&#125;,&#x27;%&#x27;)</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>重点：参考记得取别名，方便识别和使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据视频id查找视频对象</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> videoId</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">Video <span class="title">selectById</span><span class="params">(<span class="meta">@Param(&quot;video_id&quot;)</span> <span class="keyword">int</span> videoId)</span></span>;</span><br><span class="line">    <span class="comment">//Video selectById(int videoId);</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据评分和标题模糊查询</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> point</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> title</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">List&lt;Video&gt; <span class="title">selectByPointAndTitleLike</span><span class="params">(<span class="meta">@Param(&quot;point&quot;)</span> <span class="keyword">double</span> point, <span class="meta">@Param(&quot;title&quot;)</span> String title)</span></span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<hr>
<h3 id="Mybatis配置驼峰字段映射java对象和数据库字段"><a href="#Mybatis配置驼峰字段映射java对象和数据库字段" class="headerlink" title="Mybatis配置驼峰字段映射java对象和数据库字段"></a>Mybatis配置驼峰字段映射java对象和数据库字段</h3><ul>
<li>数据库字段是下划线，java属性是驼峰，如何查询映射上去<ul>
<li>方法一：<code>select cover_img as coverImg from video</code> 多字段如何处理</li>
</ul>
</li>
<li>Mybatis自带配置（加载配置文件顶部）</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--下划线自动映射驼峰字段--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">settings</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;mapUnderscoreToCamelCase&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></span><br></pre></td></tr></table></figure>



<hr>
<h3 id="Mybatis入参paramaeterType的取值类型"><a href="#Mybatis入参paramaeterType的取值类型" class="headerlink" title="Mybatis入参paramaeterType的取值类型"></a>Mybatis入参paramaeterType的取值类型</h3><ul>
<li><p>parameterType 参数类型</p>
<ul>
<li>可以是基本类型</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">parameterType=&quot;java.lang.Integer&quot;</span><br><span class="line">parameterType=&quot;java.lang.String&quot;</span><br></pre></td></tr></table></figure>

<ul>
<li>可以是java集合List或者Map</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">parameterType=&quot;java.util.Map&quot;</span><br><span class="line">parameterType=&quot;java.util.List&quot;</span><br></pre></td></tr></table></figure>

<ul>
<li>可以是java自定义对象</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">parameterType=&quot;com.moneylee.xdstudy_02.domain.Video&quot;</span><br></pre></td></tr></table></figure></li>
<li><p>取参数值，具体某个字段的类型，从java类型映射到数据库类型</p>
<ul>
<li>例子 #(title, jdbcType=VARCHAR)</li>
<li>注意<ul>
<li>多数情况下不加正常使用，但是如果出现报错：无效的列类型，则是缺少jdbcType；</li>
<li>只有当字段可以为NULL时才需要jdbcType属性</li>
</ul>
</li>
<li>常见的数据库类型和java列席对比</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=&quot;selectByPointAndTitleLike&quot;  resultType=&quot;com.moneylee.xdstudy_02.domain.Video&quot;&gt;</span><br><span class="line">  </span><br><span class="line">       select * from video where point=#&#123;point, jdbcType=DOUBLE&#125; and title like concat(&#x27;%&#x27;, #&#123;title, jdbcType=VARCHAR&#125;,&#x27;%&#x27;)</span><br><span class="line">  </span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></li>
<li><p>常见的数据库类型和java列席对比</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">JDBC Type								Java Type</span><br><span class="line">CHAR										String</span><br><span class="line">VARCHAR									String	</span><br><span class="line">LONGVARCHAR							String</span><br><span class="line">NUMERIC									java.math.BigDecimal</span><br><span class="line">DECIMAL									java.math.BigDecimal</span><br><span class="line">BIT											boolean</span><br><span class="line">BOOLEAN									boolean	</span><br><span class="line">TINYINT									byte</span><br><span class="line">SMALLINT								short</span><br><span class="line">INTEGER									INTEGER</span><br><span class="line">INTEGER									int</span><br><span class="line">BIGINT									long</span><br><span class="line">REAL										float</span><br><span class="line">FLOAT										double</span><br><span class="line">DOUBLE									double</span><br><span class="line">BINARY									byte[]</span><br><span class="line">VARBINARY								byte[]</span><br><span class="line">LONGVARBINARY						byte[]</span><br><span class="line">DATE										java.sql.Date</span><br><span class="line">TIME										java.sql.Time</span><br><span class="line">TIMESTAMP								java.sql.Timestamp</span><br><span class="line">CLOB										Clob</span><br><span class="line">BLOB										Blob</span><br><span class="line">ARRAY										Array</span><br><span class="line">DISTINCT								mapping of underlying type</span><br><span class="line">STRUCT									Struct</span><br><span class="line">REF											Ref</span><br><span class="line">DATALINK								java.net.URL</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<hr>
<h3 id="Mybatis插入语法之新增和自增主键"><a href="#Mybatis插入语法之新增和自增主键" class="headerlink" title="Mybatis插入语法之新增和自增主键"></a>Mybatis插入语法之新增和自增主键</h3><ul>
<li><p>新增一条记录</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;add&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.moneylee.xdstudy_02.domain.Video&quot;</span> <span class="attr">useGeneratedKeys</span>=<span class="string">&quot;true&quot;</span> <span class="attr">keyProperty</span>=<span class="string">&quot;id&quot;</span> <span class="attr">keyColumn</span>=<span class="string">&quot;id&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        INSERT INTO `video`(`title`, `summary`, `cover_img`, `price`, `create_time`, `point`)</span><br><span class="line">         VALUES</span><br><span class="line">          (#&#123;title,jdbcType=VARCHAR&#125;, #&#123;summary,jdbcType=VARCHAR&#125;, #&#123;coverImg,jdbcType=VARCHAR&#125;, #&#123;price,jdbcType=INTEGER&#125;,</span><br><span class="line">           #&#123;createTime,jdbcType=TIMESTAMP&#125;,#&#123;point,jdbcType=DOUBLE&#125;);</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>如何获得插入的自增主键</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;add&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.moneylee.xdstudy_02.domain.Video&quot;</span> <span class="attr">useGeneratedKeys</span>=<span class="string">&quot;true&quot;</span> <span class="attr">keyProperty</span>=<span class="string">&quot;id&quot;</span> <span class="attr">keyColumn</span>=<span class="string">&quot;id&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//新增一条记录</span></span><br><span class="line">Video video = <span class="keyword">new</span> Video();</span><br><span class="line">video.setTitle(<span class="string">&quot;测试title&quot;</span>);</span><br><span class="line">video.setCoverImg(<span class="string">&quot;xdclass.net/aaa.png&quot;</span>);</span><br><span class="line">video.setPoint(<span class="number">9.4</span>);</span><br><span class="line">video.setCreateTime(<span class="keyword">new</span> Date());</span><br><span class="line">video.setPrice(<span class="number">99</span>);</span><br><span class="line">video.setSummary(<span class="string">&quot;这是面试专题概要&quot;</span>);</span><br><span class="line"><span class="keyword">int</span> rows = videoMapper.add(video);</span><br><span class="line">System.out.println(rows);</span><br><span class="line">System.out.println(video.toString());</span><br></pre></td></tr></table></figure></li>
</ul>
<hr>
<h3 id="Mybatis-foreach批量插入语法和使用"><a href="#Mybatis-foreach批量插入语法和使用" class="headerlink" title="Mybatis foreach批量插入语法和使用"></a>Mybatis foreach批量插入语法和使用</h3><ul>
<li>批量插入多条视频记录</li>
<li>foreach： 用于循环拼接的内置标签，常用于 批量新增，in查询等常见</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">包含以下属性：</span><br><span class="line">	collection：必填，值为要迭代循环的集合类型，情况有多种，入参是List类型的时候，collection属性值为list</span><br><span class="line">	入参是Map类型的时候，collection属性值为map的key值</span><br><span class="line">	</span><br><span class="line">	item：每个元素迭代时的别名</span><br><span class="line">	index：索引的属性名，在集合数组情况下为当前索引值，当迭代对象为map时，这个值是map的key</span><br><span class="line">	open：整个循环内容的开头字符串</span><br><span class="line">	close：整个循环内容的结尾字符串</span><br><span class="line">	separator：每次循环的分隔符</span><br></pre></td></tr></table></figure>

<ul>
<li>例子</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;addBatch&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.moneylee.xdstudy_02.domain.Video&quot;</span> <span class="attr">useGeneratedKeys</span>=<span class="string">&quot;true&quot;</span> <span class="attr">keyProperty</span>=<span class="string">&quot;id&quot;</span> <span class="attr">keyColumn</span>=<span class="string">&quot;id&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        INSERT INTO `video`(`title`, `summary`, `cover_img`, `price`, `create_time`, `point`)</span><br><span class="line">         VALUES</span><br><span class="line">         <span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">&quot;list&quot;</span> <span class="attr">item</span>=<span class="string">&quot;video&quot;</span> <span class="attr">index</span>=<span class="string">&quot;index&quot;</span> <span class="attr">separator</span>=<span class="string">&quot;,&quot;</span>&gt;</span></span><br><span class="line">          (#&#123;video.title,jdbcType=VARCHAR&#125;, #&#123;video.summary,jdbcType=VARCHAR&#125;, #&#123;video.coverImg,jdbcType=VARCHAR&#125;, #&#123;video.price,jdbcType=INTEGER&#125;,</span><br><span class="line">           #&#123;video.createTime,jdbcType=TIMESTAMP&#125;,#&#123;video.point,jdbcType=DOUBLE&#125;)</span><br><span class="line">         <span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>如果批量插入要获取自增id，可以按照单挑记录获得自增id的方式</li>
</ul>
<hr>
<h3 id="Mybatis3-x更新和删除"><a href="#Mybatis3-x更新和删除" class="headerlink" title="Mybatis3.x更新和删除"></a>Mybatis3.x更新和删除</h3><ul>
<li>update 更新对象</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;updateVideo&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.moneylee.xdstudy_02.domain.Video&quot;</span>&gt;</span></span><br><span class="line">        update video</span><br><span class="line"></span><br><span class="line">        set</span><br><span class="line">        title = #&#123;title,jdbcType=VARCHAR&#125;,</span><br><span class="line">        summary = #&#123;summary,jdbcType=VARCHAR&#125;,</span><br><span class="line">        cover_img = #&#123;coverImg,jdbcType=VARCHAR&#125;,</span><br><span class="line">        price = #&#123;price,jdbcType=INTEGER&#125;,</span><br><span class="line">        create_time = #&#123;createTime,jdbcType=TIMESTAMP&#125;,</span><br><span class="line">        point = #&#123;point,jdbcType=DOUBLE&#125;</span><br><span class="line">        where</span><br><span class="line">        id = #&#123;id&#125;</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li><p>存在其他不想被更新的字段却设置为null值或者默认值了</p>
</li>
<li><p>if test 标签：</p>
<ul>
<li>if标签可以通过判断传入的值来确定查询条件， test指定一个OGNL表达式</li>
<li>常见写法</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">//当前字段符合条件才更新这个字段的值</span><br><span class="line">&lt;!--选择性更新--&gt;</span><br><span class="line">    &lt;update id=&quot;updateVideoSelect&quot; parameterType=&quot;com.moneylee.xdstudy_02.domain.Video&quot;&gt;</span><br><span class="line">        update video</span><br><span class="line">        &lt;trim prefix=&quot;set&quot; suffixOverrides=&quot;,&quot;&gt;</span><br><span class="line"></span><br><span class="line">        &lt;if test=&quot;title != null&quot;&gt;title = #&#123;title,jdbcType=VARCHAR&#125;,&lt;/if&gt;</span><br><span class="line">        &lt;if test=&quot;summary != null&quot;&gt;summary = #&#123;summary,jdbcType=VARCHAR&#125;,&lt;/if&gt;</span><br><span class="line">        &lt;if test=&quot;coverImg != null&quot;&gt;cover_img = #&#123;coverImg,jdbcType=VARCHAR&#125;,&lt;/if&gt;</span><br><span class="line">        &lt;if test=&quot;price != 0&quot;&gt;price = #&#123;price,jdbcType=INTEGER&#125;,&lt;/if&gt;</span><br><span class="line">        &lt;if test=&quot;createTime != null&quot;&gt;create_time = #&#123;createTime,jdbcType=TIMESTAMP&#125;,&lt;/if&gt;</span><br><span class="line">        &lt;!-- 特别注意： 一定要看pojo类里面是基本数据类型还是包装数据类型 --&gt;</span><br><span class="line">        &lt;if test=&quot;point != null&quot;&gt;point = #&#123;point,jdbcType=DOUBLE&#125;,&lt;/if&gt;</span><br><span class="line">        &lt;/trim&gt;</span><br><span class="line">        where</span><br><span class="line">        id = #&#123;id&#125;</span><br><span class="line">&lt;/update&gt;</span><br></pre></td></tr></table></figure></li>
<li><p>Mybatis3.x的删除语法和操作</p>
<ul>
<li><p>delete删除语法</p>
<ul>
<li>需求：删除某个字段 且金额大于10元的数据</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;delete id=&quot;deleteByCreateTimeAndPrice&quot; parameterType=&quot;java.util.Map&quot;&gt;</span><br><span class="line"></span><br><span class="line">        delete from video where create_time &lt;![CDATA[ &gt; ]]&gt; #&#123;createTime&#125; and price &lt;![CDATA[ &lt; ]]&gt; #&#123;price&#125;</span><br><span class="line"></span><br><span class="line">&lt;/delete&gt;</span><br></pre></td></tr></table></figure></li>
<li><p>由于mybatis的sql写在xml里面，有些sql的语句和xml冲突</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">大于等于	&lt;![CDATA[ &gt;= ]]&gt;</span><br><span class="line">小于等于 	&lt;![CDATA[ &lt;= ]]&gt;</span><br></pre></td></tr></table></figure></li>
</ul>
<hr>
<h3 id="Mybatis3-x-查询typeAlias别名的使用"><a href="#Mybatis3-x-查询typeAlias别名的使用" class="headerlink" title="Mybatis3.x 查询typeAlias别名的使用"></a>Mybatis3.x 查询typeAlias别名的使用</h3><ul>
<li><p>typeAlias</p>
<ul>
<li>类型别名，给类取个别名，可以不用输入类的全路径</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">&quot;com.moneylee.xdstudy_02.domain.Video&quot;</span> <span class="attr">alias</span>=<span class="string">&quot;Video&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;updateVideoSelect&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Video&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>如果有很多类，是否需要一个一个配置</p>
<ul>
<li>使用包扫描即可</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.moneylee.xdstudy_02.domain&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;updateVideoSelect&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Video&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>本身就内置很多别名，比如 Integer、String、List、Map等</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">delete</span> <span class="attr">id</span>=<span class="string">&quot;deleteByCreateTimeAndPrice&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Map&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<hr>
<h3 id="Mybatis3-x-SQL片段的使用"><a href="#Mybatis3-x-SQL片段的使用" class="headerlink" title="Mybatis3.x SQL片段的使用"></a>Mybatis3.x SQL片段的使用</h3><ul>
<li><p>你是否常用<code>select *</code>去查询数据库</p>
<ul>
<li>小项目没问题，高并发项目不推荐这样使用，查询性能低，应该选择需要的字段</li>
</ul>
</li>
<li><p>什么是sql片段</p>
<ul>
<li>根据业务需要，自定制要查询的字段，并可以复用</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">&quot;base_video_filed&quot;</span>&gt;</span></span><br><span class="line">    title,summary,cover_img</span><br><span class="line"><span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Integer&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;Video&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    -- select * from video where id = #&#123;videoId&#125;</span><br><span class="line">    -- select title,summary,cover_img from video where id = #&#123;videoId&#125;</span><br><span class="line">    select <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">&quot;base_video_filed&quot;</span>/&gt;</span> from video where id = #&#123;videoId&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<hr>
<h3 id="Mybatis3-x-复杂sql查询"><a href="#Mybatis3-x-复杂sql查询" class="headerlink" title="Mybatis3.x 复杂sql查询"></a>Mybatis3.x 复杂sql查询</h3><ul>
<li>Mybatis的SQL语句返回结果有两种<ul>
<li>resultType<ul>
<li>查询出的字段在相应的pojo中必须有和它相同的字段对应，或者基本数据类型</li>
<li>适合简单查询</li>
</ul>
</li>
<li>resultMap<ul>
<li>需要自定义字段，或者多表查询，比resultType更强大</li>
<li>适合复杂查询</li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">    id 指定查询列的唯一标识</span></span><br><span class="line"><span class="comment">    colum 数据库字段名称</span></span><br><span class="line"><span class="comment">    property pojo类的名称</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;VideoResultMap&quot;</span> <span class="attr">type</span>=<span class="string">&quot;Video&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;INTEGER&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;video_title&quot;</span> <span class="attr">property</span>=<span class="string">&quot;title&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;VARCHAR&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;summary&quot;</span> <span class="attr">property</span>=<span class="string">&quot;summary&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;VARCHAR&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;cover_img&quot;</span> <span class="attr">property</span>=<span class="string">&quot;coverImg&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;VARCHAR&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectBaseFiledByIdWithResultMap&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;VideoResultMap&quot;</span>&gt;</span></span><br><span class="line">     select id, title as video_title, summary, cover_img from video where id = #&#123;video_id&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>



<hr>
<h3 id="ResultMap-复杂对象一对一查询结果映射之association"><a href="#ResultMap-复杂对象一对一查询结果映射之association" class="headerlink" title="ResultMap 复杂对象一对一查询结果映射之association"></a>ResultMap 复杂对象一对一查询结果映射之association</h3><ul>
<li>association：映射到pojo的某个复杂类型属性，比如订单order对象里面包含user对象</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta">        <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">        <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">&quot;com.moneylee.xdstudy_02.dao.VideoOrderMapper&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;VideoOrderMapper&quot;</span> <span class="attr">type</span>=<span class="string">&quot;VideoOrder&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;user_id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;userId&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;out_trade_no&quot;</span> <span class="attr">property</span>=<span class="string">&quot;outTradeNo&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;create_time&quot;</span> <span class="attr">property</span>=<span class="string">&quot;createTime&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;state&quot;</span> <span class="attr">property</span>=<span class="string">&quot;state&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;total_fee&quot;</span> <span class="attr">property</span>=<span class="string">&quot;totalFee&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;video_id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;videoId&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;video_title&quot;</span> <span class="attr">property</span>=<span class="string">&quot;videoTitle&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">            association 配置属性一对一</span></span><br><span class="line"><span class="comment">            property 对应videoOrder里面的user属性名</span></span><br><span class="line"><span class="comment">            javaType 这个属性的类型</span></span><br><span class="line"><span class="comment">         --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;user&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;user_id&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;name&quot;</span> <span class="attr">column</span>=<span class="string">&quot;name&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;headImg&quot;</span> <span class="attr">column</span>=<span class="string">&quot;head_img&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;createTime&quot;</span> <span class="attr">column</span>=<span class="string">&quot;create_time&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;phone&quot;</span> <span class="attr">column</span>=<span class="string">&quot;phone&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">association</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;queryVideoOrder&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;VideoOrderMapper&quot;</span>&gt;</span></span><br><span class="line">        select</span><br><span class="line">         o.id id,</span><br><span class="line">         o.user_id,</span><br><span class="line">         o.out_trade_no,</span><br><span class="line">         o.create_time,</span><br><span class="line">         o.state,</span><br><span class="line">         o.total_fee,</span><br><span class="line">         o.video_id,</span><br><span class="line">         o.video_title,</span><br><span class="line">         u.name,</span><br><span class="line">         u.head_img,</span><br><span class="line">         u.create_time,</span><br><span class="line">         u.phone</span><br><span class="line"></span><br><span class="line">        from video_order o left join user u on o.user_id = u.id</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">VideoOrderMapper orderMapper = sqlSession.getMapper(VideoOrderMapper.class);</span><br><span class="line">List&lt;VideoOrder&gt; orderList = orderMapper.queryVideoOrder();</span><br><span class="line">System.out.println(orderList.toString());</span><br></pre></td></tr></table></figure>



<hr>
<h3 id="ResultMap复杂对象一对多查询结果映射之collection"><a href="#ResultMap复杂对象一对多查询结果映射之collection" class="headerlink" title="ResultMap复杂对象一对多查询结果映射之collection"></a>ResultMap复杂对象一对多查询结果映射之collection</h3><ul>
<li>collection：一对多查询结果映射，比如user有多个订单</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;UserOrderResultMap&quot;</span> <span class="attr">type</span>=<span class="string">&quot;User&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;name&quot;</span> <span class="attr">column</span>=<span class="string">&quot;name&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;headImg&quot;</span> <span class="attr">column</span>=<span class="string">&quot;head_img&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;createTime&quot;</span> <span class="attr">column</span>=<span class="string">&quot;create_time&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">&quot;phone&quot;</span> <span class="attr">column</span>=<span class="string">&quot;phone&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">        property pojo类中集合属性名称</span></span><br><span class="line"><span class="comment">        ofType 集合里面的pojo对象</span></span><br><span class="line"><span class="comment">     --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">&quot;videoOrderList&quot;</span> <span class="attr">ofType</span>=<span class="string">&quot;VideoOrder&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置主键 管理order的唯一标识 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;order_id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;user_id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;userId&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;out_trade_no&quot;</span> <span class="attr">property</span>=<span class="string">&quot;outTradeNo&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;create_time&quot;</span> <span class="attr">property</span>=<span class="string">&quot;createTime&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;state&quot;</span> <span class="attr">property</span>=<span class="string">&quot;state&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;total_fee&quot;</span> <span class="attr">property</span>=<span class="string">&quot;totalFee&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;video_id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;videoId&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;video_title&quot;</span> <span class="attr">property</span>=<span class="string">&quot;videoTitle&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;queryUserOrder&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;UserOrderResultMap&quot;</span>&gt;</span></span><br><span class="line">    select</span><br><span class="line">    u.id,</span><br><span class="line">    u.name,</span><br><span class="line">    u.head_img,</span><br><span class="line">    u.create_time,</span><br><span class="line">    u.phone,</span><br><span class="line">    o.id order_id,</span><br><span class="line">    o.out_trade_no,</span><br><span class="line">    o.user_id,</span><br><span class="line">    o.create_time,</span><br><span class="line">    o.state,</span><br><span class="line">    o.total_fee,</span><br><span class="line">    o.video_id,</span><br><span class="line">    o.video_title</span><br><span class="line">    from user u left join video_order o on u.id = o.user_id</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">VideoOrderMapper orderMapper = sqlSession.getMapper(VideoOrderMapper.class);</span><br><span class="line">List&lt;User&gt; users = orderMapper.queryUserOrder();</span><br><span class="line">System.out.println(users.toString());</span><br></pre></td></tr></table></figure>



<hr>
<h3 id="Mybatis3-x-ResultMap复杂对象查询总结"><a href="#Mybatis3-x-ResultMap复杂对象查询总结" class="headerlink" title="Mybatis3.x ResultMap复杂对象查询总结"></a>Mybatis3.x ResultMap复杂对象查询总结</h3><ul>
<li>Assocation 映射的是一个pojo类，处理一对一的关联关系</li>
<li>Collection 映射的是一个集合列表，处理的是一对多的关系</li>
<li>模板：</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="comment">&lt;!-- column不不做限制，可以为任意表的字段，⽽而property须为type 定义的pojo属性--&gt;</span> <span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;唯⼀一的标识&quot;</span> <span class="attr">type</span>=<span class="string">&quot;映射的pojo对象&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;表的主键字段,或查询语句句中的别名字段&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;字段类型&quot;</span> <span class="attr">property</span>=<span class="string">&quot;映射 pojo对象的主键属性&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;表的⼀一个字段&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;字段类型&quot;</span> <span class="attr">property</span>=<span class="string">&quot;映射到pojo对象的⼀一个属 性&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;pojo的⼀一个对象属性&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;pojo关联的pojo对象&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;关联pojo对象对应表的主键字段&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;字段类型&quot;</span> <span class="attr">property</span>=<span class="string">&quot;关联pojo 对象的属性&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;表的字段&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;字段类型&quot;</span> <span class="attr">property</span>=<span class="string">&quot;关联pojo对象的属性&quot;</span>/&gt;</span> <span class="tag">&lt;/<span class="name">association</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 集合中的property 需要为oftype定义的pojo对象的属性--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">collection</span> <span class="attr">property</span>=<span class="string">&quot;pojo的集合属性名称&quot;</span> <span class="attr">ofType</span>=<span class="string">&quot;集合中单个的pojo对象类型&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;集合中pojo对象对应在表的主键字段&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;字段类型&quot;</span> <span class="attr">property</span>=<span class="string">&quot;集合 中pojo对象的主键属性&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;任意表的字段&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;字段类型&quot;</span> <span class="attr">property</span>=<span class="string">&quot;集合中的pojo对象的属 性&quot;</span> /&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">collection</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>



<hr>
<h3 id="Mybatis3-x多级缓存和懒加载"><a href="#Mybatis3-x多级缓存和懒加载" class="headerlink" title="Mybatis3.x多级缓存和懒加载"></a>Mybatis3.x多级缓存和懒加载</h3><ul>
<li>什么是缓存<ul>
<li>程序经常要调用的对象在内存中，方便其使用时可以快速调用，不必去数据库或者其它持久化设备中查询，主要就是提高性能</li>
</ul>
</li>
<li>Mybatis一级缓存<ul>
<li>简介：一级缓存的作用域是SQLSession，同一个SQLSession中执行相同的SQL查询（相同的SQL和参数），第一次回去查询数据库并写在缓存中，第二次会直接从缓存内读取</li>
<li>基于PerprtualCache的HashMap本地缓存</li>
<li>默认开启一级缓存</li>
</ul>
</li>
<li>失效策略：当执行SQL时 两次查询中间发生了增删改查的操作，即insert、update、delete等操作commit后会清空该SQLSession缓存</li>
</ul>
<hr>
<h3 id="Mybatis3-x二级缓存的使用-上"><a href="#Mybatis3-x二级缓存的使用-上" class="headerlink" title="Mybatis3.x二级缓存的使用(上)"></a>Mybatis3.x二级缓存的使用(上)</h3><ul>
<li>Mybatis二级缓存：<ul>
<li>简介：二级缓存是namespace级别的，多个SqlSession去操作同一个namespace下的Mapper的sql语句，多个SqlSession可以共用二级缓存，如果两个mapper的namespace相同，（即使是两个mapper，那么这两个mapper中执行sql查询到的数据也将存在相同的二级缓存区域中，但是最后是每个mapper的单独的名称空间）</li>
<li>基于PerpetualCache的HashMap本地缓存,可自定义存储源,如Ecache/Redis等</li>
<li>默认是没有开启二级缓存</li>
<li>操作流程：第一次调用某个namespace下的SQL去查询信息，查询到的信息会存放在该mapper对应的二级缓存区域，第二次调用同个namespace下的mapper映射文件中，相同的sql去查询信息，会去对应的二级缓存区域去取</li>
</ul>
</li>
<li>失效策略：执行同个namespace下的mapper映射文件中增删改查sql，并执行了commit操作，会清空该二级缓存</li>
<li>注意：实现二级缓存的时候，Mybatis建议返回的POJO是可序列化的，也就是建议实现Serializable接口</li>
<li>缓存淘汰策略：会使用默认的LRU算法来收回（最近最少使用的）</li>
<li>如何实现某个二级缓存mapper.xml里面配置</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--开启mapper的namespace下的⼆级缓存--&gt;</span> <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">eviction:代表的是缓存回收策略，常⻅下⾯两种。</span></span><br><span class="line"><span class="comment">(1) LRU,最近最少使用的，⼀处最⻓时间不用的对象 (2) FIFO,先进先出，按对象进入缓存的顺序来移除他们</span></span><br><span class="line"><span class="comment">flushInterval:刷新间隔时间，单位为毫秒，这里配置的是100秒刷新，如果不不配置 它，当SQL被执行的时候才会去刷新缓存。</span></span><br><span class="line"><span class="comment">size:引⽤用数⽬目，代表缓存最多可以存储多少个对象，设置过大会导致内存溢出 readOnly:只读，缓存数据只能读取⽽而不不能修改，默认值是false</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">cache</span> <span class="attr">eviction</span>=<span class="string">&quot;LRU&quot;</span> <span class="attr">flushInterval</span>=<span class="string">&quot;100000&quot;</span> <span class="attr">readOnly</span>=<span class="string">&quot;true&quot;</span> <span class="attr">size</span>=<span class="string">&quot;1024&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">全局配置:</span><br><span class="line"><span class="tag">&lt;<span class="name">settings</span>&gt;</span> </span><br><span class="line">  <span class="comment">&lt;!--这个配置使全局的映射器器(⼆级缓存)启用或禁用缓存，全局总开关，这⾥里里关闭，mapper中开启了也没⽤用--&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;cacheEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></span><br></pre></td></tr></table></figure>

<hr>
<h3 id="Mybatis3-X-二级缓存使用-下"><a href="#Mybatis3-X-二级缓存使用-下" class="headerlink" title="Mybatis3.X 二级缓存使用(下)"></a><strong>Mybatis3.X</strong> 二级缓存使用(下)</h3><ul>
<li> 配置实操</li>
<li>如果需要控制全局mapper⾥面某个⽅法不使⽤缓存，可以配置 useCache=”false” </li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"> <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectById&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;java.lang.Integer&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;Video&quot;</span> <span class="attr">useCache</span>=<span class="string">&quot;false&quot;</span>&gt;</span></span><br><span class="line">select <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">&quot;base_video_field&quot;</span>/&gt;</span> from video where id = # &#123;video_id,jdbcType=INTEGER&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>一级缓存和⼆级缓存使⽤顺序 <ul>
<li>优先查询二级缓存-》查询一级缓存-》数据库 </li>
</ul>
</li>
</ul>
<hr>
<h3 id="Mybatis3-X-懒加载"><a href="#Mybatis3-X-懒加载" class="headerlink" title="Mybatis3.X 懒加载"></a><strong>Mybatis3.X</strong> 懒加载</h3><ul>
<li>什么是懒加载: 按需加载，先从单表查询，需要时再从关联表去关联查询，能⼤大提⾼数据库性能,并不是所有场景下使⽤用懒加载都能提⾼效率</li>
<li>Mybatis懒加载: resultMap里面的association、collection有延迟加载功能 </li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--全局参数设置--&gt;</span> </span><br><span class="line"><span class="tag">&lt;<span class="name">settings</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--延迟加载总开关--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;lazyLoadingEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line"><span class="comment">&lt;!--将aggressiveLazyLoading设置为false表示按需加载，默认为true--&gt;</span> </span><br><span class="line"><span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;aggressiveLazyLoading&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;VideoOrderResultMapLazy&quot;</span> <span class="attr">type</span>=<span class="string">&quot;VideoOrder&quot;</span>&gt;</span> <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;user_id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;userId&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;out_trade_no&quot;</span> <span class="attr">property</span>=<span class="string">&quot;outTradeNo&quot;</span>/&gt;</span> <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;create_time&quot;</span> <span class="attr">property</span>=<span class="string">&quot;createTime&quot;</span>/&gt;</span> <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;state&quot;</span> <span class="attr">property</span>=<span class="string">&quot;state&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;total_fee&quot;</span> <span class="attr">property</span>=<span class="string">&quot;totalFee&quot;</span>/&gt;</span> <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;video_id&quot;</span> <span class="attr">property</span>=<span class="string">&quot;videoId&quot;</span>/&gt;</span> <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;video_title&quot;</span> <span class="attr">property</span>=<span class="string">&quot;videoTitle&quot;</span>/&gt;</span> <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;video_img&quot;</span> <span class="attr">property</span>=<span class="string">&quot;videoImg&quot;</span>/&gt;</span></span><br><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">select: 指定延迟加载需要执行的statement id column: 和select查询关联的字段</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;<span class="name">association</span> <span class="attr">property</span>=<span class="string">&quot;user&quot;</span> <span class="attr">javaType</span>=<span class="string">&quot;User&quot;</span> <span class="attr">column</span>=<span class="string">&quot;user_id&quot;</span> <span class="attr">select</span>=<span class="string">&quot;findUserByUserId&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--一对一管理查询订单， 订单内部包含⽤用户属性 懒加载--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;queryVideoOrderListLazy&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;VideoOrderResultMapLazy&quot;</span>&gt;</span></span><br><span class="line">select</span><br><span class="line">o.id id,</span><br><span class="line">o.user_id , o.out_trade_no, o.create_time, o.state, o.total_fee, o.video_id, o.video_title, o.video_img</span><br><span class="line">from video_order o</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;findUserByUserId&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;User&quot;</span>&gt;</span> select * from user where id=#&#123;id&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>dubug模式测试懒加载不准确，可以直接run </li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">// resultmap association关联查询(测试懒加载)</span></span><br><span class="line">VideoOrderMapper videoOrderMapper = sqlSession.getMapper(VideoOrderMapper.class);</span><br><span class="line">List&lt;VideoOrder&gt; videoOrderList = videoOrderMapper.queryVideoOrderListLazy(); System.out.println(videoOrderList.size());</span><br><span class="line"><span class="comment">//课程里⾯演示是6条订单记录，但是只查询3次用户信息，是因为部分⽤户信息⾛了一级缓存 sqlsession</span></span><br><span class="line"><span class="keyword">for</span>(VideoOrder videoOrder : videoOrderList)&#123;</span><br><span class="line">System.out.println(videoOrder.getVideoTitle());</span><br><span class="line">System.out.println(videoOrder.getUser().getName()); &#125;</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="MyBatis3-X整合Mysql数据库事务"><a href="#MyBatis3-X整合Mysql数据库事务" class="headerlink" title="MyBatis3.X整合Mysql数据库事务"></a><strong>MyBatis3.X</strong>整合<strong>Mysql</strong>数据库事务</h3><p>讲解<strong>Mybatis</strong>的事务管理形式 </p>
<ul>
<li><p>使⽤用<strong>JDBC</strong>的事务管理</p>
<ul>
<li><p>使⽤用 java.sql.Connection对象完成对事务的提交(commit())、回滚(rollback())、关闭 </p>
<p>(close()) </p>
</li>
</ul>
</li>
<li><p>使⽤用<strong>MANAGED</strong>的事务管理</p>
</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">&quot;development&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">&quot;development&quot;</span>&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- &lt;transactionManager type=&quot;JDBC&quot;/&gt;--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">&quot;MANAGED&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">&quot;POOLED&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driver&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;url&quot;</span> <span class="attr">value</span>=<span class="string">&quot;jdbc:mysql://127.0.0.1:3306/xdclass?useUnicode=true<span class="symbol">&amp;amp;</span>characterEncoding=utf-8<span class="symbol">&amp;amp;</span>useSSL=false&quot;</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;root&quot;</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;12345678&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>事务工厂TransactionFactory的两个实现类<ul>
<li>JdbcTransactionFactory -&gt; JdbcTransaction</li>
<li>ManagedTransactionFactory -&gt; ManagedTransaction</li>
</ul>
</li>
<li>注意：如果不是web程序，然后使用的事务管理形式是MANAGED，那么将没有事务管理能力</li>
</ul>
<hr>
<h3 id="Mysql的Innodb和MyISAM引擎的区别"><a href="#Mysql的Innodb和MyISAM引擎的区别" class="headerlink" title="Mysql的Innodb和MyISAM引擎的区别"></a><strong>Mysql</strong>的<strong>Innodb</strong>和<strong>MyISAM</strong>引擎的区别</h3><p>简介:讲解<strong>mysql</strong>常⻅见的两种存储引擎的区别 </p>
<table>
<thead>
<tr>
<th>区别项</th>
<th>Innodb</th>
<th>myisam</th>
</tr>
</thead>
<tbody><tr>
<td>事务</td>
<td>支持</td>
<td>不支持</td>
</tr>
<tr>
<td>锁粒度</td>
<td>行锁，适合⾼高并发</td>
<td>表锁，不适合高并发</td>
</tr>
<tr>
<td>是否默认</td>
<td>默认</td>
<td>非默认</td>
</tr>
<tr>
<td>支持外键</td>
<td>支持外键</td>
<td>不⽀持</td>
</tr>
<tr>
<td>适合场景</td>
<td>读写均衡,写⼤大于读场景，需要事务</td>
<td>读多写少场景，不需要事务</td>
</tr>
<tr>
<td>全⽂文索引</td>
<td>可以通过插件实现, 更更多使⽤用ElasticSearch</td>
<td>支持全⽂文索引</td>
</tr>
</tbody></table>
<p><strong>重点:MyISAM不不⽀支持事务，如果需要事务则改为innodb引擎 更改数据库的表里面的引擎</strong> </p>
<ul>
<li><p>为什么原先没进行commit操作，也可以插入成功? </p>
<ul>
<li>因为原先是myisam引擎,没有事务，直接插入成功 </li>
</ul>
</li>
<li><p>检查数据库的 引擎 ，改为innodb</p>
<ul>
<li>多个表video/chapter/episode/user/video_order </li>
<li>video_banner </li>
</ul>
</li>
<li><p>案例例 </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">事务管理理记得改为这个mybatis-config.xml</span><br><span class="line"><span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">&quot;JDBC&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>事务管理形式 MANAGED，设置⾮非⾃自动提交，然后注释 commit, 依旧可以保存成功 </p>
</li>
<li><p>不用重点关注，公司开发项⽬的事务控制基本是交给Spring，或者使用分布式事务 </p>
</li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">李财</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://moneylee.gitee.io/blog/2020/06/08/Java%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0_04/">https://moneylee.gitee.io/blog/2020/06/08/Java%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0_04/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://moneylee.gitee.io/blog" target="_blank">MoneyLee’s Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2020/06/08/Java%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0_03/"><img class="prev-cover" src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Java项目开发笔记_03</div></div></a></div><div class="next-post pull-right"><a href="/blog/2020/06/07/Java%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0_02/"><img class="next-cover" src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java项目开发笔记_02</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2019/11/10/SYvXMgCFuTNOVeZ.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">李财</div><div class="author-info__description">嗨，我是李财 (MoneyLee)， 一名来自中国的 iOS开发者。</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">47</div></a></div><div class="card-info-data-item is-center"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/licai28" target="_blank" title=""><i class="fa fa-github"></i></a><a class="social-icon" href="mailto:licai_1024@hotmail.com" target="_blank" title=""><i class="fa fa-envelope"></i></a><a class="social-icon" href="/blog/img/mywechat.jpg" target="_blank" title=""><i class="fa fa-weixin"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%BE%E8%AE%A1%E5%BA%94%E7%94%A8%E7%9A%84%E6%A0%B8%E5%BF%83%E6%95%B0%E6%8D%AE%E5%BA%93%E8%A1%A8-ER%E5%9B%BE"><span class="toc-number">1.</span> <span class="toc-text">设计应用的核心数据库表-ER图</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JavaWeb%E9%80%9A%E8%BF%87%E5%8E%9F%E7%94%9Fjdbc%E8%AE%BF%E9%97%AE%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">1.1.</span> <span class="toc-text">JavaWeb通过原生jdbc访问数据库</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8E%9F%E7%94%9FJDBC%E8%AE%BF%E9%97%AE%E6%95%B0%E6%8D%AE%E5%BA%93%E7%BC%BA%E7%82%B9%E5%92%8CORM%E6%A1%86%E6%9E%B6%E4%BB%8B%E7%BB%8D"><span class="toc-number">2.</span> <span class="toc-text">原生JDBC访问数据库缺点和ORM框架介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B0%E7%89%88ORM%E6%A1%86%E6%9E%B6Mybatis3-x%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="toc-number">2.1.</span> <span class="toc-text">新版ORM框架Mybatis3.x基础知识</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis%E5%BC%80%E5%8F%91%E5%BF%85%E5%A4%87%E6%8E%A7%E5%88%B6%E5%8F%B0%E6%89%93%E5%8D%B0SQL"><span class="toc-number">2.2.</span> <span class="toc-text">Mybatis开发必备控制台打印SQL</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis%E5%8F%82%E6%95%B0%E5%88%AB%E5%90%8D%E4%BD%BF%E7%94%A8"><span class="toc-number">2.3.</span> <span class="toc-text">Mybatis参数别名使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis%E9%85%8D%E7%BD%AE%E9%A9%BC%E5%B3%B0%E5%AD%97%E6%AE%B5%E6%98%A0%E5%B0%84java%E5%AF%B9%E8%B1%A1%E5%92%8C%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AD%97%E6%AE%B5"><span class="toc-number">2.4.</span> <span class="toc-text">Mybatis配置驼峰字段映射java对象和数据库字段</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis%E5%85%A5%E5%8F%82paramaeterType%E7%9A%84%E5%8F%96%E5%80%BC%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.5.</span> <span class="toc-text">Mybatis入参paramaeterType的取值类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis%E6%8F%92%E5%85%A5%E8%AF%AD%E6%B3%95%E4%B9%8B%E6%96%B0%E5%A2%9E%E5%92%8C%E8%87%AA%E5%A2%9E%E4%B8%BB%E9%94%AE"><span class="toc-number">2.6.</span> <span class="toc-text">Mybatis插入语法之新增和自增主键</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis-foreach%E6%89%B9%E9%87%8F%E6%8F%92%E5%85%A5%E8%AF%AD%E6%B3%95%E5%92%8C%E4%BD%BF%E7%94%A8"><span class="toc-number">2.7.</span> <span class="toc-text">Mybatis foreach批量插入语法和使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-x%E6%9B%B4%E6%96%B0%E5%92%8C%E5%88%A0%E9%99%A4"><span class="toc-number">2.8.</span> <span class="toc-text">Mybatis3.x更新和删除</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-x-%E6%9F%A5%E8%AF%A2typeAlias%E5%88%AB%E5%90%8D%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">2.9.</span> <span class="toc-text">Mybatis3.x 查询typeAlias别名的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-x-SQL%E7%89%87%E6%AE%B5%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">2.10.</span> <span class="toc-text">Mybatis3.x SQL片段的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-x-%E5%A4%8D%E6%9D%82sql%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.11.</span> <span class="toc-text">Mybatis3.x 复杂sql查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ResultMap-%E5%A4%8D%E6%9D%82%E5%AF%B9%E8%B1%A1%E4%B8%80%E5%AF%B9%E4%B8%80%E6%9F%A5%E8%AF%A2%E7%BB%93%E6%9E%9C%E6%98%A0%E5%B0%84%E4%B9%8Bassociation"><span class="toc-number">2.12.</span> <span class="toc-text">ResultMap 复杂对象一对一查询结果映射之association</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ResultMap%E5%A4%8D%E6%9D%82%E5%AF%B9%E8%B1%A1%E4%B8%80%E5%AF%B9%E5%A4%9A%E6%9F%A5%E8%AF%A2%E7%BB%93%E6%9E%9C%E6%98%A0%E5%B0%84%E4%B9%8Bcollection"><span class="toc-number">2.13.</span> <span class="toc-text">ResultMap复杂对象一对多查询结果映射之collection</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-x-ResultMap%E5%A4%8D%E6%9D%82%E5%AF%B9%E8%B1%A1%E6%9F%A5%E8%AF%A2%E6%80%BB%E7%BB%93"><span class="toc-number">2.14.</span> <span class="toc-text">Mybatis3.x ResultMap复杂对象查询总结</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-x%E5%A4%9A%E7%BA%A7%E7%BC%93%E5%AD%98%E5%92%8C%E6%87%92%E5%8A%A0%E8%BD%BD"><span class="toc-number">2.15.</span> <span class="toc-text">Mybatis3.x多级缓存和懒加载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-x%E4%BA%8C%E7%BA%A7%E7%BC%93%E5%AD%98%E7%9A%84%E4%BD%BF%E7%94%A8-%E4%B8%8A"><span class="toc-number">2.16.</span> <span class="toc-text">Mybatis3.x二级缓存的使用(上)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-X-%E4%BA%8C%E7%BA%A7%E7%BC%93%E5%AD%98%E4%BD%BF%E7%94%A8-%E4%B8%8B"><span class="toc-number">2.17.</span> <span class="toc-text">Mybatis3.X 二级缓存使用(下)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mybatis3-X-%E6%87%92%E5%8A%A0%E8%BD%BD"><span class="toc-number">2.18.</span> <span class="toc-text">Mybatis3.X 懒加载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#MyBatis3-X%E6%95%B4%E5%90%88Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BA%8B%E5%8A%A1"><span class="toc-number">2.19.</span> <span class="toc-text">MyBatis3.X整合Mysql数据库事务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mysql%E7%9A%84Innodb%E5%92%8CMyISAM%E5%BC%95%E6%93%8E%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">2.20.</span> <span class="toc-text">Mysql的Innodb和MyISAM引擎的区别</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/09/14/Flutter%20%E5%90%AF%E5%8A%A8%E9%A1%B5%E9%85%8D%E7%BD%AE/" title="无题"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/blog/2021/09/14/Flutter%20%E5%90%AF%E5%8A%A8%E9%A1%B5%E9%85%8D%E7%BD%AE/" title="无题">无题</a><time datetime="2021-09-14T03:05:25.000Z" title="发表于 2021-09-14 11:05:25">2021-09-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" title="iOS机器学习"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="iOS机器学习"/></a><div class="content"><a class="title" href="/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" title="iOS机器学习">iOS机器学习</a><time datetime="2020-08-21T02:04:10.000Z" title="发表于 2020-08-21 10:04:10">2020-08-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/20/Docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" title="Docker 基础知识整理"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Docker 基础知识整理"/></a><div class="content"><a class="title" href="/blog/2020/08/20/Docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" title="Docker 基础知识整理">Docker 基础知识整理</a><time datetime="2020-08-20T12:12:02.000Z" title="发表于 2020-08-20 20:12:02">2020-08-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/19/Flutter/" title="Flutter"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Flutter"/></a><div class="content"><a class="title" href="/blog/2020/08/19/Flutter/" title="Flutter">Flutter</a><time datetime="2020-08-19T02:04:03.000Z" title="发表于 2020-08-19 10:04:03">2020-08-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/19/iOS%20AR%E5%92%8CARKit4/" title="iOS AR和ARKit4"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="iOS AR和ARKit4"/></a><div class="content"><a class="title" href="/blog/2020/08/19/iOS%20AR%E5%92%8CARKit4/" title="iOS AR和ARKit4">iOS AR和ARKit4</a><time datetime="2020-08-19T02:04:03.000Z" title="发表于 2020-08-19 10:04:03">2020-08-19</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 李财</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>